<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "600px"
    },
    chartList: {
      type: Array,
      required: true
    },
    chartData: {
      type: Object,
      required: true
    },
    titleInfo: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.chart.dispose();
        this.setOptions(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    getRandomColor() {
      const r = Math.floor(Math.random() * 255);
      const g = Math.floor(Math.random() * 255);
      const b = Math.floor(Math.random() * 255);
      return "rgba(" + r + "," + g + "," + b + ",1)";
    },
    initChart() {
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      //console.log("chartData.xTitle", chartData);
      //console.log("chartData.xTitle", chartData.xTitle);
      console.log(this.chartList);
      this.chart = echarts.init(this.$el, "macarons");
      const titleInfo = this.titleInfo;
      // 组装显示的数据线
      const series = [];
      for (const i of this.chartList) {
        let color = '';
        if (i.prop == "zxList"){
          color = "#68B3C8";
        } else  if (i.prop == "lclList"){
          color = "#F3BB45";
        } else  if (i.prop == "fclList"){
          color = "#675bba";
        } else  if (i.prop == "expressList"){
          color = "red";
        } else {
          color = this.getRandomColor();
        }
        let obj = {
          name: i.label,
          itemStyle: {
            normal: {
              color: color,
              lineStyle: {
                color: color,
                width: 2
              }
            }
          },
          smooth: true,
          type: "line",
          data: chartData[i.prop],
          animationDuration: 2800,
          animationEasing: "cubicInOut"
        };

        series.push(obj);
      }

      this.chart.setOption({
        title: {
          text: titleInfo.text,
          subtext: titleInfo.subtext
        },
        xAxis: {
          data: chartData.xTitle,
          // boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        toolbox: {
          show: true,
          feature: {
            dataView: { readOnly: false },
            magicType: { type: ["line", "bar"] },
            restore: {},
            saveAsImage: {}
          }
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          },
          padding: [5, 10],
          formatter: function(params) {
            let newParams = [];
            let tooltipString = [];
            newParams = [...params];

            newParams.sort((a, b) => {
              return b.value - a.value;
            });
            newParams.forEach(p => {
              const cont =
                p.marker + " " + p.seriesName + ": " + p.value + "<br/>";
              tooltipString.push(cont);
            });
            return tooltipString.join("");
          }
        },
        yAxis: {
          type: "value",
          axisPointer: {
            snap: true
          }
        },
        legend: {
          data: titleInfo.legend
        },
        series: series
      });
    }
  }
};
</script>
